<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/05/24
 * Time: 上午10:14
 * @Description  [王者荣耀活动]
 */
class KingGlorySupportController extends BaseController{

    /**
     * @Author:          sqq
     * @DateTime:        2018-05-24 上午11:40
     * @Description:     王者荣耀    [ 战队信息查询 ]
     * @link http://www.activity.com/v1/activity/kingglorysupport/teamlist
     */
    public function teamlist(){
//        $validator = Validator::make(Input::all(),
//            array(
//                'token' => 'required',
//            )
//        );
//        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
//            if ($validator->fails()) {
//                $response = Lang::get("scs_error.login_token_error_21005");
//                break;
//            }
            //传入参数
//            $token = Input::get('token');
//            $response = array('code' => 0, 'msg' => 'ok');
//            $sc_user=$this->verificationUserToken($token,$response);
//            if(is_null($sc_user)){
//                break;
//            }
            //$teamlist = KingGloryTeam::rightJoin('kingglory_store','kingglory_team.group','=','kingglory_store.id')->select("kingglory_team.id","kingglory_team.name","kingglory_team.lcon","group","kingglory_store.id as shop_id","kingglory_store.name as shop_name","kingglory_store.lcon as shop_lcon")->get();
            $teamlist = KingGloryTeam::select("id","name","lcon","shop_id","content")->groupBy("name")->orderBy("created_at","asc")->get()->toArray();
            foreach($teamlist as $key => $val){
                $shop_id = explode(',',$val['shop_id']);
                $shop = KingGloryStore::whereIn("id",$shop_id)->select("id","name","lcon")->get()->toArray();
                $num = KingGlorySupportCount::where("team_id",$val['id'])->select("num")->first();
                if($num['num'] == null || $num['num'] == '0' || $num['num'] == ''){
                    $num['num'] = '0';
                }
                $teamlist[$key]['num'] = $num['num'];
                foreach($shop as $k => $v){
                    $teamlist[$key]['shop'][$k]['shop_id'] = $v['id'];
                    $teamlist[$key]['shop'][$k]['shop_name'] = $v['name'];
                    $teamlist[$key]['shop'][$k]['shop_lcon'] = $v['lcon'];
                }
                unset($teamlist[$key]['shop_id']);
            }

            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("team_info"=>$teamlist));
        } while (false);
        Log::info("info", array("system" => "activity" ,"model" => "kingglorysupport", "event" => "teamlist", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:          sqq
     * @DateTime:        2018-05-24 上午11:40
     * @Description:     王者荣耀    [ 支持 ]
     * @link http://www.activity.com/v1/activity/kingglorysupport/support?token=817765e840c84f00afd7141f453ce3ff&team_id=3&anchors=erlou&source=1
     */
    public function support(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'team_id' => 'required',
                'anchors' => 'required',
                'source' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("activity_error.login_token_error_1005");
                break;
            }
            $messages = $validator->messages();
            $res = $this->verificationUserToken(Input::get("token"),$response);
            if ($res['status'] == 0) {
                $response = $res['response'];
                break;
            }
            $teamlist = KingGloryTeam::select("id")->get()->toArray();
            $teamlist = array_column($teamlist,'id');
            //传入参数
            $team_id = Input::get('team_id');
            $anchors = Input::get('anchors');
            $source = Input::get('source');
            $type = Input::get('type',1);
            if(!in_array($team_id, $teamlist)){
                $response = Lang::get('activity_error.activity_team_exit_error_1001');
                break;
            }
            //判断活动是否存在
            $is_activity = ActivityBaseInfo::getActivityBaseInfo(Config::get('config.king_activity_login_id'));
            if(!$is_activity || Config::get('config.king_login_coupons') != 1){
                $response = Lang::get('activity_error.activity_king_exit_error_1002');
                break;
            }else{
                //活动存在
                $new_time = date("Y-m-d H:i:s",time()); //获取当前时间
                //判断活动是否开始结束
                if($new_time < $is_activity->begin_date){
                    //当前时间小于活动开始时间，活动未开始
                    $response = Lang::get('activity_error.activity_king_exit_error_1003');
                    break;
                }elseif($new_time > $is_activity->end_date){
                    //当前时间大于活动结束时间，活动已结束
                    $response = Lang::get('activity_error.activity_king_exit_error_1004');
                    break;
                }else{
                    //活动进行中
                    //判断是否领取优惠券
                    $activity = Coupons::getNewUserRegister(Config::get('config.king_activity_login_id'),$response);
                    if(!is_null($activity)){
                        foreach ($activity as  $v) {
                            $coupon_base_info = Coupons::getCouponBaseInfo($v->coupon_base_info_id);
                            if (!$coupon_base_info) {
                                break;
                            }
                            if ($coupon_base_info->status == 0) {
                                break;
                            }
                            if ($coupon_base_info->date_type == 0 && strtotime($coupon_base_info->end_date.' 23:59:59') < strtotime(date('Y-m-d H:i:s', time()))) { //起止时间
                                break;
                            }
                            $user_coupons = coupons::isCoupons($v->coupon_base_info_id,$res['id']);
                            if(!is_null($user_coupons)){  //如果已经领过一次了，就不能在领了
                                $response = Lang::get('activity_error.activity_king_exit_error_1006');
                                break;
                            }else{
                                //未领取优惠券
                                $coupon_whether_exist = Coupons::getCouponOther($v->coupon_base_info_id);
                                if (!is_null($coupon_whether_exist)) {
                                    $coupons = Coupons::find($coupon_whether_exist->id);
                                    $coupons->status = 2;
                                    $coupons->received_user_id = $res['id'];
                                    $coupons->get_time = date("Y-m-d H:i:s", time());
                                    if ($coupon_base_info->date_type == 1) { //顺延日期
                                        $coupons->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));
                                    }
                                    $coupons->save();
                                    //友盟优惠券到账消息推送
                                    if(Config::get('u_meng_push.is_open_u_meng_push') ==1 && Input::get('register_source_id') ==2){
                                        Orders::notifyMsg($res['id'],2,'优惠券已到账','您的'.($coupon_base_info->amount/100).'元优惠券已到帐');
                                    }
                                    $res2 = KingGlorySupport::where('user_id', $res['id'])->first();
                                    if(is_null($res2)){
                                        $res1 = KingGlorySupportCount::where("team_id", $team_id)->first();
                                        $support = KingGlorySupport::insertcount($team_id,$res['id'],$anchors,$source,$type);
                                        if(empty($res1)){
                                            $support = KingGlorySupportCount::insertcount($team_id);
                                        }else{
                                            $support = KingGlorySupportCount::updatecount($team_id,$res1['num']);
                                        }
                                    }else{
                                        $support = KingGlorySupportCount::where("team_id",$team_id)->select("team_id","num")->first();
                                    }
                                    $response = array('code' => 0, 'msg' => 'ok', 'result' => array("support"=>$support));
                                }else{
                                    $response = Lang::get('activity_error.activity_king_exit_error_1007');
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        } while (false);
        Log::info("info", array("system" => "activity" ,"model" => "kingglorysupport", "event" => "support", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:          sqq
     * @DateTime:        2018-05-25 下午14:30
     * @Description:     王者荣耀    [ 浏览量 ]
     * @link http://www.activity.com/v1/activity/kingglorysupport/browse?anchors=erlou&source=1
     */
    public function browse(){
        $validator = Validator::make(Input::all(),
            array(
                'anchors' => 'required',
                'source' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do{
            $messages = $validator->messages();
            if($messages->has('anchors')) {
                $response = Lang::get('activity_error.activity_anchors_exit_error_2000');
                break;
            }
            if($messages->has('source')) {
                $response = Lang::get('activity_error.activity_source_exit_error_2001');
                break;
            }
            //获取锚点
            $anchors = Input::get("anchors");
            //获取来源
            $source = Input::get("source");
            //查询浏览量表是否有锚点信息
            $browseone = KingGloryBrowse::BrowseOne($anchors,$source);;
            //判断是否存在，存在就+1，不存在就创建
            if(is_null($browseone)){
                $browse = KingGloryBrowse::InsertBrowse($anchors,$source);
            }else{
                $browse = KingGloryBrowse::UpdatedBrowse($browseone['num'],$browseone['id']);
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("support"=>$browse));
        } while (false);
        Log::info("info", array("system" => "activity" ,"model" => "kingglorysupport", "event" => "browse", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:          sqq
     * @DateTime:        2018-05-25 下午14:30
     * @Description:     王者荣耀    [ 流失统计 ]
     * @link http://www.activity.com/v1/activity/kingglorysupport/browse?anchors=erlou&source=1
     */
    public function SupportLoss(){
        $validator = Validator::make(Input::all(),
            array(
                'anchors' => 'required',
                'source' => 'required',
                'team_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do{
            if ($validator->fails()) {
                $response = Lang::get("activity_error.login_token_error_1005");
                break;
            }
            //获取信息
            $anchors = Input::get("anchors");
            $source = Input::get("source");
            $team_id = Input::get("team_id");
            $type = Input::get("type",1);
            $loss = KingGloryLoss::InsertLoss($anchors,$source,$team_id,$type);
            if(is_null($loss)){
                $supportloss = KingGloryLossCount::InsertLoss($anchors,$source,$team_id);
            }else{
                $supportloss = KingGloryLossCount::UpdatedLoss($anchors,$source,$team_id);
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => array("support"=>$supportloss));
        } while (false);
        Log::info("info", array("system" => "activity" ,"model" => "kingglorysupport", "event" => "SupportLoss", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

}